home *** CD-ROM | disk | FTP | other *** search
- Path: news.cc.sunysb.edu!ghauser
- From: ghauser@ic.sunysb.edu (George Hauser)
- Newsgroups: comp.lang.c
- Subject: Is this a C BUG??? (A string issue)
- Date: 31 Mar 1996 01:39:59 GMT
- Organization: State University of New York at Stony Brook
- Message-ID: <4jknpf$9k3@abel.cc.sunysb.edu>
- NNTP-Posting-Host: csws5.cc.sunysb.edu
-
-
- Hi....
-
- After hours trying to debug the program that I include in the bottom
- I gave up and fixed the problem by checking for a string with more than two
- characters, before I wrote to an external file.
-
- I am reading the whole line in one shot using fgets(). I need to pad with
- blank spaces those records that are < 194, until the string is set to 194.
-
- Sometimes my records are already filled with 194 and sometimes not.
-
- When I get the string of a line that contains 195 chars (data + eoln) it
- returns a line of 193 and then the next line returns 2 characters....
-
- This is WRONG fgets should return 194 characters... but it seems like it
- finds a NULL on space 193 and then returns the end of line and another null.
-
- I have spent too much time with this so now even though its ugly I simply
- check to see if the lenght of the string is greater than 2.
-
- If someone sees something wrong with the code, I'd appreciate the advice.
- But I think this is some obscure messed up hidden character.
-
- BTW: I'm using Borlands C++ 4.52 compiler in a Win95 pc.
-
- Thank you.
-
- - George
-
- Here's the buggy code.
- -----------------
-
-
- #define linemax 194 /* The predifined lenght of the record */
- void rmgarbage(char *str)
- {
- int counter;
- counter = 0;
- while(str[counter] != '\0' && counter < linemax)
- {
- if (str[counter] == '*')
- str[counter] = ' ';
- counter++;
- }
- }
-
- void fillrecords(char *str)
- {
- int counter;
- for (counter = strlen(str) - 1; counter < linemax; counter++)
- str[counter] = ' ';
- }
-
- void main(int argc, char *argv[])
- {
- FILE *sourcefp, *destfp; /*Source and destination pointers */
- char line[linemax + 1]; /* The lenght of the line plus NULL terminator */
- long int records; /* Number of records that can be passed to ext */
-
- records = 0;
- while(fgets(line,linemax,sourcefp) != NULL)
- {
- if (strlen(line) > 2) /* TO CORRECT THE ERROR IN THE STRING RETURN!!!!!*/
- {
- if (ferror(sourcefp))
- {
- printf("\nError while reading source file.\n");
- exit(1);
- }
- rmgarbage(line); /* Remove bad chars */
- fillrecords(line); /* Format the record line */
- fputs(line,destfp); /* Write to disk */
- fputc('\n',destfp);
- records++; /* Increment our counter */
- }
- }
-
- fclose(sourcefp); /* Close source file. */
- fclose(destfp); /* Close destination file. */
- printf("Total Number Of Records Processed: %d \n", records);
- } /* End */
-